import java.util.HashMap;
import java.util.Map;
//最长定差子序列
//测试链接：https://leetcode.cn/problems/longest-arithmetic-subsequence-of-given-difference/description/
public class LongestSubsequence {
    public int longestSubsequence(int[] arr, int difference) {
        // 创建⼀个哈希表，在哈希表中做 dp
        Map<Integer, Integer> hash = new HashMap<Integer, Integer>(); // arr[i], dp[i]
        int ret = 1;
        for (int a : arr) {
            hash.put(a, hash.getOrDefault(a - difference, 0) + 1);
            ret = Math.max(ret, hash.get(a));
        }
        return ret;
    }
}
